/*
 * Copyright 2008 Max Kugland
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.splink.asbeangen.templates.as3;

import java.util.ArrayList;
import java.util.List;

import org.splink.asbeangen.templates.ClassTemplateHelper;
import org.splink.asbeangen.templates.IClassTemplate;
import org.splink.asbeangen.templates.ITemplateClass;

public class AS3Class implements ITemplateClass {
	public static final int VAR_TYPE_CUSTOM = 0;
	public static final int VAR_TYPE_NATIVE = 1;
	public static final int VAR_TYPE_PCDATA = 2;

	public static final String DATA_CLASS_POSTFIX = "Data";
	public static final String READER_CLASS_POSTFIX = "Reader";

	protected IClassTemplate _template;
	protected List<As3Member> _memberNames = new ArrayList<As3Member>();
	protected String _definition;
	protected String _imports;
	protected String _clazz;
	protected String _constructorHead;
	protected String _constructorBody;
	protected String _methods = "";
	protected String _members = "";
	protected String _classname;
	protected String _packagename;
	protected List<String> _copyright;

	public AS3Class(List<String> copyright, String packagename, String classname, List<String> interfazes,
			String ifazeBasePackage, IClassTemplate template) {
		_packagename = ClassTemplateHelper.firstCharToLowerCaseArray(packagename);
		_classname = classname;
		_template = template;
		_copyright = copyright;

		_definition = _template.getOpenPackage(_packagename);

		_imports = "";
		// only data classes implement interfaces
		if (interfazes.size() > 0 && classname.indexOf(READER_CLASS_POSTFIX) == -1) {
			_clazz = _template.getOpenClassImplements(_classname, interfazes);
			for (String ifaceName : interfazes) {
				_imports += _template.getImport(ifazeBasePackage + "." + ifaceName);
			}
		} else {
			_clazz = _template.getOpenClass(_classname);
		}
		_constructorHead = _template.getOpenConstructorHead(_classname);
		_constructorBody = _template.getOpenConstructorBody(_classname);
	}

	public String getExtension() {
		return "as";
	}

	public String getID() {
		return ClassTemplateHelper.firstCharToLowerCase(_classname).split(DATA_CLASS_POSTFIX)[0];
	}

	public String getClassname() {
		return _classname;
	}

	public String getPackagename() {
		return _packagename;
	}

	public IClassTemplate getTemplate() {
		return _template;
	}

	public void addToImports(String classpath) {
		String imp = _template.getImport(ClassTemplateHelper.firstCharToLowerCaseArray(classpath + DATA_CLASS_POSTFIX));
		if (!_imports.contains(imp)) {
			_imports += imp;
		}
	}

	public void addToConstructor(String name, String type, int vartype) {
		if (vartype == VAR_TYPE_CUSTOM) {
			name += DATA_CLASS_POSTFIX;
			type = handleCustomType(type + DATA_CLASS_POSTFIX);
		} else if (vartype == VAR_TYPE_PCDATA) {
			name = "content";
			type = "String";
		}

		String head = _template.getParameter(name, type);
		String body = _template.getMemberAllocation(name);
		if (!_constructorHead.contains(head)) {
			_constructorHead += head;
		}
		if (!_constructorBody.contains(body)) {
			_constructorBody += body;
		}
	}

	public void addToMethods(String name, String type, int vartype) {
	}

	public void addToAccessMethods(String name, String accessor, String type) {
	}

	public void addToMembers(String name, String type, int vartype) {
		String originalname = name;

		if (vartype == VAR_TYPE_CUSTOM) {
			name += DATA_CLASS_POSTFIX;
			type = handleCustomType(type + DATA_CLASS_POSTFIX);
		} else if (vartype == VAR_TYPE_PCDATA) {
			name = "content";
			type = "String";
		}
		
		
		As3Member m = new As3Member(originalname, ClassTemplateHelper.firstCharToLowerCase(name), type, vartype);
		if(!_memberNames.contains(m)) {
			_memberNames.add(m);
		}

		String member = _template.getMember(name, type);
		if (!_members.contains(member)) {
			_members += member;
		}
	}

	public String getDefinition() {
		return "override me!";
	}

	protected String handleCustomType(String type) {
		if (!type.equals("Array" + DATA_CLASS_POSTFIX)) {
			return ClassTemplateHelper.firstCharToUpperCase(type);
		} else
			return "Array";
	}

	public String getImports() {
		return _imports;
	}

	public String getMembers() {
		return _members;
	}

	public String getMethods() {
		return _methods;
	}
}